package applet.attendee.locked.components.summary;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

import data.CMSRes;
import data.account.attendee.AttendeeAccount;
import data.account.attendee.lodging.LodgingData;
import data.account.attendee.travel.TravelData;
import data.event.Event;
import data.nametag.NametagGenerator;

import applet.attendee.CMSAttendeeView;
import applet.attendee.locked.components.schedule.ScheduleDialog;
import applet.misc.graphics.AttendeeBackgroundPanel;

public class CMSAttendeeCheckedInSummary extends AttendeeBackgroundPanel
{
	private static final long serialVersionUID = -3561011979804965351L;
	
	private CMSAttendeeView attendeeView;
	public CMSAttendeeView attendeeView() { return attendeeView; }
	
	public CMSAttendeeCheckedInSummary(CMSAttendeeView attendeeView)
	{
		super(new GridBagLayout());
		this.attendeeView = attendeeView;
		setBorder(new EmptyBorder(25, 25, 25, 25));
		
		JPanel summaryDialog = new JPanel(new BorderLayout());
		summaryDialog.setBorder(new BevelBorder(BevelBorder.RAISED));
		
			JPanel titlePane = new JPanel(new BorderLayout());
			titlePane.setBackground(CMSRes.Colors.get("Theme Color Medium"));
			titlePane.setBorder(new EmptyBorder(10, 10, 10, 10));
				JLabel title = new JLabel("Conference Overview:");
				title.setFont(title.getFont().deriveFont(Font.BOLD));
				titlePane.add(title, BorderLayout.WEST);
		summaryDialog.add(titlePane, BorderLayout.NORTH);
		summaryDialog.add(getContentPanel(attendeeView.attendee()), BorderLayout.CENTER);
		summaryDialog.add(getCheckOutPanel(), BorderLayout.SOUTH);
		
		add(summaryDialog);
	}
	
	private JPanel getContentPanel(AttendeeAccount attendee)
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.add(getPersonalInfoPanel(attendee), BorderLayout.NORTH);
		
		if(attendee.travel() != null)
		{
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.add(panel, BorderLayout.NORTH);
			layoutHelper.add(getTravelInfoPanel(attendee.travel()), BorderLayout.SOUTH);
			panel = layoutHelper;
		}
		
		if(attendee.lodging() != null)
		{
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.add(panel, BorderLayout.NORTH);
			layoutHelper.add(getLodgingInfoPanel(attendee.lodging()), BorderLayout.SOUTH);
			panel = layoutHelper;
		}
		
		JPanel layoutHelper = new JPanel(new BorderLayout());
		layoutHelper.add(panel, BorderLayout.NORTH);
		layoutHelper.add(getUtilPanel(), BorderLayout.SOUTH);
		layoutHelper.setBorder(new CompoundBorder(new BevelBorder(BevelBorder.LOWERED), new EmptyBorder(5, 5, 5, 5)));
		return layoutHelper;
	}
	
	private JPanel getPersonalInfoPanel(AttendeeAccount attendee)
	{
		JPanel panel = new JPanel(new GridLayout(0, 2));
		panel.setBorder(new CompoundBorder(new EmptyBorder(2, 2, 2, 2), new LineBorder(Color.LIGHT_GRAY, 1)));
		
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				JLabel labelHelper = new JLabel("Personal Information:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		panel.add(new JPanel());
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Name:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel(attendee.fullName());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Organization:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel(attendee.org());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		return panel;
	}
	
	private JPanel getLodgingInfoPanel(LodgingData lodging)
	{
		JPanel panel = new JPanel(new GridLayout(0, 2));
		panel.setBorder(new CompoundBorder(new EmptyBorder(2, 2, 2, 2), new LineBorder(Color.LIGHT_GRAY, 1)));

			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				JLabel labelHelper = new JLabel("Lodging Information:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		panel.add(new JPanel());
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Hotel:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel("Holiday Inn");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Floor:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel("" + lodging.floor());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);

			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Room:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel("Suite " + lodging.room());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
	
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Arrival Date:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel(lodging.startDate());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Duration of Stay:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel("" + lodging.duration() + " days");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Meal Plan:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel(lodging.mealPlan());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
	
		return panel;
	}
	
	private JPanel getTravelInfoPanel(TravelData travel)
	{
		JPanel panel = new JPanel(new GridLayout(0, 2));
		panel.setBorder(new CompoundBorder(new EmptyBorder(2, 2, 2, 2), new LineBorder(Color.LIGHT_GRAY, 1)));

			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				JLabel labelHelper = new JLabel("Travel Information:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		panel.add(new JPanel());
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Airline:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel(travel.airline());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Arrival Date:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel(travel.arrivalDate());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
		
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 2));
				labelHelper = new JLabel("Arrival Time:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setOpaque(false);
			layoutHelper.setBorder(new EmptyBorder(10, 2, 10, 10));
				labelHelper = new JLabel(travel.arrivalTime());
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper);
	
		return panel;
	}
	
	private JPanel getUtilPanel()
	{
		JPanel panel = new JPanel(new GridLayout(1, 2));
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 10));
				JButton nametagButton = new JButton("Generate Nametag");
				nametagButton.setPreferredSize(new Dimension(200, 30));
				nametagButton.addActionListener(new NametagGenerationListener());
			layoutHelper.add(nametagButton, BorderLayout.CENTER);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 10, 10, 10));
				JButton scheduleButton = new JButton("View Event Schedule");
				scheduleButton.setPreferredSize(new Dimension(200, 30));
				scheduleButton.addActionListener(new ScheduleViewListener());
			layoutHelper.add(scheduleButton, BorderLayout.CENTER);
		panel.add(layoutHelper);
		return panel;
	}
	
	private JPanel getCheckOutPanel()
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBackground(CMSRes.Colors.get("Theme Color Medium"));
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
			JButton checkoutButton = new JButton("Check Out");
			checkoutButton.setPreferredSize(new Dimension(200, 50));
			checkoutButton.addActionListener(new CheckOutListener());
		panel.add(checkoutButton, BorderLayout.EAST);
		return panel;
	}
	
	private class NametagGenerationListener implements ActionListener
	{
		@Override public void actionPerformed(ActionEvent e)
		{
			JFileChooser fC = new JFileChooser();
	        int returnVal = fC.showSaveDialog(CMSRes.frame());
	        if (returnVal == JFileChooser.APPROVE_OPTION)
	            try
	        	{
					ImageIO.write(NametagGenerator.genNametag(attendeeView.attendee(), 800, 400),
							"PNG", fC.getSelectedFile());
					
					JOptionPane.showMessageDialog(CMSRes.frame(),
							"Your nametag has been created and saved to the specified file.", 
							"Nametag Saved", JOptionPane.INFORMATION_MESSAGE);
				} catch (IOException e1) { e1.printStackTrace(); }
		}
	}
	
	private class ScheduleViewListener implements ActionListener
	{
		@Override public void actionPerformed(ActionEvent e)
		{
			ScheduleDialog dialog = new ScheduleDialog(attendeeView.attendee());
			dialog.setLocationRelativeTo(null);
			dialog.setVisible(true);
		}
	}
	
	private class CheckOutListener implements ActionListener
	{
		@Override public void actionPerformed(ActionEvent e)
		{
			String[] options = new String[] { "Cancel Check Out", "Finish Check Out" };
			int n = JOptionPane.showOptionDialog(CMSRes.frame(),
					"Checking out will cause all user data to be deleted. It " +
					"cannot be reversed. Are you sure you wish to check out?",
				    "Warning!", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,
				    null, options, options[0]);
			if(n == 1)
			{
				for(Event event : CMSRes.conference().events())
					event.attendees().remove(attendeeView().attendee().userID());
				CMSRes.conference().saveData();
				attendeeView().attendee().checkOut();
				attendeeView().viewController().deferToDefaultView();
			}
		}
	}
}